package com.adams.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adams.access.db.bean.User;
import com.adams.encache.mechanism.CacheUtil;
import com.adams.user.dao.UserDao;
import com.adams.user.service.UserService;
import com.adams.utils.Page;

/**
 * 
 * 
 * File Name : UserServiceImpl.java
 * 
 * @Description : 用户管理服务类，提供对用户的增、删、改查功能
 * @author 902990封文进
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService
{

	@Autowired
	private UserDao userDao;

	public void setUserDao(UserDao userDao)
	{
		this.userDao = userDao;
	}

	/**
	 * 
	 * Description : 创建用户
	 * 
	 * @param user
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void createUser(User user)
	{
//		user.setVersion(1);
		userDao.save(user);
		CacheUtil.clearCacheByClass(User.class);
	}

	/**
	 * 
	 * Description : 更新用户
	 * 
	 * @param user
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void updateUser(User user)
	{
		userDao.update(user);
		CacheUtil.clearCacheByClass(User.class);
	}

	/**
	 * 
	 * Description : 删除用户
	 * 
	 * @param user
	 * 
	 */
	@Transactional(rollbackFor = java.lang.Exception.class)
	public void deleteUser(User user)
	{
		userDao.delete(user);
		CacheUtil.clearCacheByClass(User.class);
	}

	/**
	 * 
	 * Description : 根据编号查询用户
	 * 
	 * @param id
	 * @return User
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public User queryById(Long id)
	{
		return userDao.get(id);
	}

	/**
	 * 
	 * Description : 根据名称查询用户
	 * 
	 * @param name
	 * @return User
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public User queryByName(String name)
	{
		return userDao.queryByName(name);
	}

	/**
	 * 
	 * Description : 根据角色查询用户
	 * 
	 * @param roleId
	 * @return List<User>
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<User> queryByRoleId(Long roleId)
	{
		return userDao.queryByRoleId(roleId);
	}

	/**
	 * 
	 * Description : 根据用户名，真实姓名和分页查询用户（用户名和真实姓名采用模糊查询）
	 * 
	 * @param page
	 * @param userName
	 * @param realName
	 * @return Page<User>
	 * 
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<User> queryAllByPage(Page<User> page, String userName,
			String realName)
	{
		return userDao.queryAllByPage(page, userName, realName);
	}

}
